/**
 * ====================================================
 * 文件名称: NewsCategoryServiceImpl.java
 * 修订记录：
 * No    日期				作者(操作:具体内容)
 * 1.    2015年9月21日			chenxy(创建:创建文件)
 * ====================================================
 * 类描述：(说明未实现或其它不应生成javadoc的内容)
 */
package com.yuexunit.finance.news.service;

import java.util.List;

import com.yuexunit.persist.annotation.Transactional;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.core.BusinessException;
import com.yuexunit.finance.common.util.DateUtils;
import com.yuexunit.finance.news.entity.NewsCategoryEntity;
import com.yuexunit.finance.news.entity.lexicon.CategoryTypeEnum;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.page.PageCriteria;
import com.yuexunit.page.PageList;
import com.yuexunit.persist.PersistUtil;
import com.yuexunit.util.DateUtil;

/**
 * @ClassName: NewsCategoryServiceImpl
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author chenxy
 * @date 2015年9月21日 下午6:37:38
 */
@Implements(NewsCategoryService.class)
public class NewsCategoryServiceImpl implements NewsCategoryService {

	/** 同名栏目验证 */
	private void validate(NewsCategoryEntity entity) {
		DetachedCriteria dc = DetachedCriteria.forClass(NewsCategoryEntity.class);
		dc.add(Restrictions.eq("categoryName", entity.getCategoryName()));
		dc.add(Restrictions.eq("enabled", true));
		List<NewsCategoryEntity> list = PersistUtil.get(dc);
		if (!list.isEmpty()) {
			throw new BusinessException("资讯栏目名称重复");
		}
	}

	@Override
	@Transactional
	public Long createNewsCategory(NewsCategoryEntity entity) {
		validate(entity);
		entity.setEnabled(true);
		entity.setCreateDateTime(DateUtil.now());
		entity.setUpdateDateTime(DateUtil.now());
		PersistUtil.insert(entity);
		return entity.getNewsCategoryId();
	}

	@Override
	@Transactional
	public void updateNewsCategory(NewsCategoryEntity entity) {
		NewsCategoryEntity e = inquireNewsCategoryDetail(entity.getNewsCategoryId());
		if (!e.getCategoryName().equals(entity.getCategoryName())) {
			validate(entity);
		}
		e.setCategoryType(entity.getCategoryType());
		e.setCategoryName(entity.getCategoryName());
		e.setUpdateDateTime(DateUtil.now());
		PersistUtil.update(e);
	}

	@Override
	@Transactional
	public void disableNewsCategory(Long newsCategoryId) {
		NewsCategoryEntity e = inquireNewsCategoryDetail(newsCategoryId);
		e.setEnabled(false);
		e.setUpdateDateTime(DateUtil.now());
		PersistUtil.update(e);
	}

	@Override
	public NewsCategoryEntity inquireNewsCategoryDetail(Long newsCategoryId) {
		NewsCategoryEntity e = PersistUtil.get(NewsCategoryEntity.class, newsCategoryId);
		if (e == null || !e.getEnabled()) {
			throw new BusinessException("资讯栏目未找到");
		}
		return e;
	}

	@Override
	public NewsCategoryEntity inquireNewsCategory(Long newsCategoryId) {
		return PersistUtil.get(NewsCategoryEntity.class, newsCategoryId);
	}

	@Override
	public PageList<NewsCategoryEntity> inquireNewsCategoryList(String categoryName, CategoryTypeEnum categoryType, String updateFromTime, String updateToTime, PageCriteria page) {
		DetachedCriteria dc = DetachedCriteria.forClass(NewsCategoryEntity.class);
		if (categoryType != null) {
			dc.add(Restrictions.eq("categoryType", categoryType));
		}
		if (StringUtils.isNotBlank(categoryName)) {
			dc.add(Restrictions.like("categoryName", "%" + categoryName + "%"));
		}
		if (StringUtils.isNotBlank(updateFromTime)) {
			dc.add(Restrictions.gt("updateDateTime", DateUtils.formatBeginDay(updateFromTime)));
		}
		if (StringUtils.isNotBlank(updateToTime)) {
			dc.add(Restrictions.lt("updateDateTime", DateUtils.formatBeginDay(updateToTime)));
		}
		dc.add(Restrictions.eq("enabled", true));
		dc.addOrder(Order.desc("updateDateTime"));
		PageList<NewsCategoryEntity> newsCategoryPageList = PersistUtil.get(dc, page);
		return newsCategoryPageList;
	}

}
